package cn.richinfo.sparkdemo;


import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;

public class WordCountJava8 {

	public static void main(String[] args) {

		SparkConf scf=new SparkConf().setAppName("java8spark").setMaster("local");

		JavaSparkContext jsc=new JavaSparkContext(scf);

		JavaRDD<String> inputRDD=jsc.textFile("file:///c:\\words.txt");
		JavaRDD<String> wordsRDD=inputRDD.flatMap(input -> Arrays.asList(input.split(" ")).iterator());
		JavaPairRDD<String,Integer> keyvaluewordRDD=wordsRDD.mapToPair(word -> new Tuple2<String,Integer>(word,1));

		JavaPairRDD<String,Integer> wordscountRDD=keyvaluewordRDD.reduceByKey((x, y) -> x + y);

		wordscountRDD.foreach(x->System.out.println(x._1+","+x._2));
	}



}
